package com.service.manage.imp;


import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;

import com.po.Adminmain;

import java.util.HashMap;

public class SessionListener implements HttpSessionListener
{
	/**
     * 该HashMap以用户名-HttpSession对象存储一个账号只能被一个人登陆的信息。 id 和session的映射
     */
    public static HashMap<String,HttpSession> user_session = new HashMap<String,HttpSession>();

    //public static HashMap<String,String> session_user = new HashMap<String,String>();
    
    @Override
   public void sessionCreated(HttpSessionEvent se) {
	// TODO Auto-generated method stub
        HttpSession session = se.getSession();

   }
    @Override
    public void sessionDestroyed(HttpSessionEvent httpSessionEvent)
    {

        HttpSession session = httpSessionEvent.getSession();

        delSession( session );
    }

    /**
     * 由于多个用户可能同时修改造成map出现问题
     * @param session
     */
    public static synchronized void delSession(HttpSession session)
    {
        if(session != null)
        {

            // 删除单一登录中记录的变量
            if(session.getAttribute( "admin" ) != null)
            {
                Adminmain admin = (Adminmain) session.getAttribute( "admin" );
                user_session.remove(admin.getAdminid());
            }
        }
    }
}

